<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/24
 * Time: 17:22
 */
namespace app\admin\controller;
use app\admin\model\Jurs;
use app\admin\model\UpFile;
use think\Controller;
use app\admin\model\Roles as Role;
use think\Db;
use think\Request;

class Roles extends Controller
{
    public function show_list(){
        return NewBuilder::make('table')
            ->addTableHeaderAllDel('roles/del')
            ->addTableHeaderAdd('roles/show_add')
            ->addTableColumn('id','text','ID')
            ->addTableColumn('role_name','text','角色名称')
            ->addTableColumn('pid','text','上级角色')
            ->addTableColumn('role','text','权限',null,null,'30%')
            ->addTableColumn('iocn','img','图标')
            ->addTableColumn('status','switch','状态',['开启','禁用'],'roles/change')
            ->addTableRightHeader('操作')
            ->addTableRight('edit','roles/show_edit','修改')
            ->addTableRight('del','roles/del','删除')
            ->addTablePage('roles/page')
            ->fetch();
    }

    /**加载数据
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function page(Request $request){
        $limit = $request->param('limit',10);
        $data = Role::where('id','>',0)
            ->paginate($limit)
            ->toArray();
        foreach ($data['data'] as $key=>$value){
            if($value['pid']==0){
                $value['pid'] = '最上级';
            }else{
                $shangji = Role::where('id',$value['pid'])->findOrEmpty()->toArray();
                if($shangji){
                    $value['pid'] = $shangji['role_name'];
                }
            }
            if($value['iocn']){
                $value['iocn'] = getFilePath($value['iocn']);
            }
            $data['data'][$key] = $value;
        }
        return json_page_response($data);
    }

    /**
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function change(Request $request){
        $id = $request->param('id');
        if(!Role::isMyPid($id)){
            return json_my_response('你没有权限',205);
        }
        $data = Role::where('id',$id)->findOrEmpty()->toArray();
        if($data['status']==1){
            $data['status'] = 0;
        }else{
            $data['status'] = 1;
        }
        if(Role::where('id',$id)->update([
            'status'=>$data['status']
        ])){
            return json_my_response('修改成功',200);
        }else{
            return json_my_response('修改失败');
        }
    }

    /**添加
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function show_add(Request $request){
        $role = Role::where('id','>',0)->select()->toArray();
        $option = [];
        foreach ($role as $value){
            $option[$value['id']] = $value['role_name'];
        }
        $array = Role::where('id',session('adminuser')['role_id'])->json(['role'])->findOrEmpty()->toArray();
        $jur =[];
        foreach ($array['role'] as $value) {
            $jurs = Jurs::where('id',$value)->findOrEmpty()->toArray();
            if($jurs){
                $jur[$jurs['id']] = $jurs['jur_name'] . '[' . $jurs['descs'] . ']';
            }
        }
        return NewBuilder::make('form')
            ->addAction('roles/add')
            ->addNone('pid',$array['id'])
            ->addInput('role_name','角色名称')
            ->addCheckbox('role','权限',null,$jur,null,null,true)
            ->addImg('iocn','图标')
            ->addSwitch('status','状态',1)
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }

    /**添加数据
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function add(Request $request){
        $data = $request->param();
        if(isset($data['status'])&&$data['status']=='on'){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        foreach ($data['role'] as $key=>$value){
            $value = (int)$value;
            $data['role'][$key] = $value;
        }
        if(Db::name(session('modular').'_roles')->json(['role'])->insert($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**加载编辑
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function show_edit(Request $request){
        $id = $request->param('id');
        if(!Role::isMyPid($id)){
            echo '权限不足';exit;
        }
        $data = Role::where('id',$id)->json(['role'])->findOrEmpty()->toArray();
        $role = Role::where('id','>',0)->select()->toArray();
        $option = [];
        foreach ($role as $value){
            $option[$value['id']] = $value['role_name'];
        }
        $array = Role::where('id',session('adminuser')['role_id'])->json(['role'])->findOrEmpty()->toArray();
        $jur =[];
        foreach ($array['role'] as $value) {
            $jurs = Jurs::where('id',$value)->findOrEmpty()->toArray();
            if($jurs){
                $jur[$jurs['id']] = $jurs['jur_name'] . '[' . $jurs['descs'] . ']';
            }
        }
        return NewBuilder::make('form')
            ->addAction('roles/edit')
            ->addNone('id',$data['id'])
            ->addNone('pid',$data['pid'])
            ->addInput('role_name','角色名称',$data['role_name'])
            ->addCheckbox('role','权限',$data['role'],$jur,null,null,true)
            ->addImg('iocn','图标',$data['iocn'])
            ->addSwitch('status','状态',$data['status'])
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }

    /**编辑数据
     * @param Request $request
     * @return \think\response\Redirect
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function edit(Request $request){
        $data = $request->param();
        if(isset($data['status'])&&$data['status']=='on'){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        foreach ($data['role'] as $key=>$value){
            $value = (int)$value;
            $data['role'][$key] = $value;
        }
        if(Db::name(session('modular').'_roles')->where('id',$data['id'])->json(['role'])->update($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }
    public function del(Request $request){
        $id = $request->param('id');
        if(is_array($id)){
            foreach ($id as $value){
                if(!Role::isMyPid($value)){
                    return json_my_response('权限不足');
                }
            }
        }else{
            if(!Role::isMyPid($id)){
                return json_my_response('权限不足');
            }
        }
        $role = Role::where('id','in',$id)->select()->toArray();
        foreach ($role as $value){
            if($value['iocn']){
                UpFile::delFile($value['iocn']);
            }
        }
        if(Role::where('id','in',$id)->delete()){
            return json_my_response('删除成功',200);
        }else{
            return json_my_response('删除失败');
        }
    }

}